import axios, { AxiosError } from '@ohos/axios';
import { promptAction } from '@kit.ArkUI';
import { AnyObject } from '../../model/HttpModel';


const request = axios.create({ baseURL: 'http://192.168.36.217:6060', timeout: 20000 })

request.interceptors.request.use(
  (config) => {
    //将来会加token参数 //登录功能加上
    return config;
  }
)

request.interceptors.response.use(
  (response) => { //成功回调，
    if (response.data.code === 200) {
      return response.data.data;
    } else {
      //响应拦截器,错误提示
      promptAction.showToast(response.data.message);
      return Promise.reject(response.data.message)
    }
  },
  (error: AxiosError) => {
    //响应拦截器,错误提示
    return Promise.reject(error.message);
  }
)

export default class Http {
  get<T>(url: string, params?: AnyObject) {
    return request.get<any, T>(url, {
      params
    });
  }


  post<T>(url: string, data?: AnyObject) {
    return request.post<any, T>(url, data);
  }

  put<T>(url: string, data?: AnyObject) {
    return request.put<any, T>(url, data);
  }

  delete<T>(url: string, params?: AnyObject) {
    return request.delete<any, T>(url, {
      params
    });
  }
}